function [blink_snips,blink_snips_dark,eye_snips,eye_snips_dark]=meta_analysis_eye_onset(proj_meta,cur_site,order,exp_inds,shad_all)

l_win=10;
r_win=5;
data_lwin=10;
data_rwin=15;
for tnd=1:size(proj_meta(cur_site).rd,2)
    for ynd=1:size(proj_meta(cur_site).rd,1)
        pupil_pos=proj_meta(cur_site).rd(ynd,tnd).pupil_pos;
        delta=sqrt(diff(pupil_pos(1,:)).^2+diff(pupil_pos(2,:)).^2);
        delta(end+1)=delta(end);
        delta_all{tnd,ynd}=delta;
    end
end

blink_snips={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    blink_ons_all=[];
    for ynd=1:size(proj_meta(cur_site).rd,1)
        blink=proj_meta(cur_site).rd(ynd,tnd).blink;
        blink(min(exp_inds{tnd}(order{tnd}==3,1)):max(exp_inds{tnd}(order{tnd}==3,2)))=[];
        blink_thresh=median(blink)-(3*std(blink));
        blink_ons=find(diff(blink<blink_thresh)==1);
        blink_ons(find(diff(blink_ons)<data_lwin+data_rwin)+1)=[];
        if ynd>1
            blink_ons(ismember(blink_ons,blink_ons_all))=[];
            blink_ons_all(end+1:end+length(blink_ons))=blink_ons;
        else
            blink_ons_all=blink_ons;
        end
    end
    blink_ons_all=sort(blink_ons_all);
    blink_ons_all(find(diff(blink_ons_all)<data_lwin+data_rwin)+1)=[];
    blink_ons_all(blink_ons_all+data_rwin>length(blink))=[];
    blink_ons_all(blink_ons_all-data_lwin<0)=[];
    cnt=1;
    blink_snips{tnd}=[];
    if ~isempty(blink_ons_all)
        for fnd=1:length(blink_ons_all)
%             if length(unique(shad_all{tnd}(blink_ons_all(fnd)-l_win:blink_ons_all(fnd)+r_win)))==1
            if sum(shad_all{tnd}(blink_ons_all(fnd)-l_win:blink_ons_all(fnd)+r_win))==0
                blink_snips{tnd}(cnt,1)=blink_ons_all(fnd)-data_lwin;
                blink_snips{tnd}(cnt,2)=blink_ons_all(fnd)+data_rwin;
                cnt=cnt+1;
            end
        end
    else
        blink_snips{tnd}=[];
    end
end

blink_snips_dark={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
    blink_ons_all=[];
    for ynd=1:size(proj_meta(cur_site).rd,1)
        blink=proj_meta(cur_site).rd(ynd,tnd).blink(min(exp_inds{tnd}(order{tnd}==3,1)):max(exp_inds{tnd}(order{tnd}==3,2)));
        blink_thresh=median(blink)-(3*std(blink));
        blink_ons=find(diff(blink<blink_thresh)==1);
        blink_ons(find(diff(blink_ons)<data_lwin+data_rwin)+1)=[];
        if ynd>1
            blink_ons(ismember(blink_ons,blink_ons_all))=[];
            blink_ons_all(end+1:end+length(blink_ons))=blink_ons;
        else
            blink_ons_all=blink_ons;
        end
    end
    blink_ons_all=sort(blink_ons_all);
    blink_ons_all(find(diff(blink_ons_all)<data_lwin+data_rwin)+1)=[];
    blink_ons_all(blink_ons_all+data_rwin>length(blink))=[];
    blink_ons_all(blink_ons_all-data_lwin<0)=[];
    blink_ons_all=blink_ons_all+max(exp_inds{tnd}(order{tnd}~=3,2));
    cnt=1;
    blink_snips_dark{tnd}=[];
    if ~isempty(blink_ons_all)
        for fnd=1:length(blink_ons_all)
            if sum(runM(blink_ons_all(fnd)-l_win:blink_ons_all(fnd)+r_win))==0 %| sum(runM(blink_ons_all(fnd)-l_win:blink_ons_all(fnd)+r_win))==1
                blink_snips_dark{tnd}(cnt,1)=blink_ons_all(fnd)-data_lwin;
                blink_snips_dark{tnd}(cnt,2)=blink_ons_all(fnd)+data_rwin;
                cnt=cnt+1;
            end
        end
    else
        blink_snips_dark{tnd}=[];
    end
end

eye_snips={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    eye_ons_all=[];
    for ynd=1:size(proj_meta(cur_site).rd,1)
        cur_delta=delta_all{tnd,ynd};
        cur_delta(min(exp_inds{tnd}(order{tnd}==3,1)):max(exp_inds{tnd}(order{tnd}==3,2)))=[];
        eye_mov_thresh=mean(cur_delta)+(5*std(cur_delta));
        eye_mov_ons=find(diff(cur_delta>eye_mov_thresh)==1);
        eye_mov_ons(find(diff(eye_mov_ons)<data_lwin+data_rwin)+1)=[];
        %         eye_mov_ons(eye_mov_ons+10>length(cur_delta))=[];
        %         eye_mov_ons(eye_mov_ons-l_win<0)=[];
        if ~isempty(blink_snips{tnd})
            rem=zeros(1,length(eye_mov_ons));
            for ind=1:length(eye_mov_ons)
                for gnd=1:size(blink_snips{tnd},1)
                    if sum(blink_snips{tnd}(gnd,1):blink_snips{tnd}(gnd,2)==eye_mov_ons(ind))>0
                        rem(ind)=1;
                    end
                end
            end
            eye_mov_ons(logical(rem))=[];
        end
        if ynd>1
            eye_mov_ons(ismember(eye_mov_ons,eye_ons_all))=[];
            eye_ons_all(end+1:end+length(eye_mov_ons))=eye_mov_ons;
        else
            eye_ons_all=eye_mov_ons;
        end
    end
    eye_ons_all=sort(eye_ons_all);
    eye_ons_all(find(diff(eye_ons_all)<data_lwin+data_rwin)+1)=[];
    eye_ons_all(eye_ons_all+data_rwin>length(cur_delta))=[];
    eye_ons_all(eye_ons_all-data_lwin<=0)=[];
    cnt=1;
    eye_snips{tnd}=[];
    if ~isempty(eye_ons_all)
        for fnd=1:length(eye_ons_all)
%             if length(unique(shad_all{tnd}(eye_ons_all(fnd)-l_win:eye_ons_all(fnd)+r_win)))==1
            if sum(shad_all{tnd}(eye_ons_all(fnd)-l_win:eye_ons_all(fnd)+r_win))==0
                eye_snips{tnd}(cnt,1)=eye_ons_all(fnd)-data_lwin;
                eye_snips{tnd}(cnt,2)=eye_ons_all(fnd)+data_rwin;
                cnt=cnt+1;
            end
        end
    else
        eye_snips{tnd}=[];
    end
end

eye_snips_dark={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
    eye_ons_all=[];
    for ynd=1:size(proj_meta(cur_site).rd,1)
        cur_delta=delta_all{tnd,ynd}(exp_inds{tnd}(order{tnd}==3,1):exp_inds{tnd}(order{tnd}==3,2));
        eye_mov_thresh=mean(cur_delta)+(5*std(cur_delta));
        eye_mov_ons=find(diff(cur_delta>eye_mov_thresh)==1);
        eye_mov_ons(find(diff(eye_mov_ons)<data_lwin+data_rwin)+1)=[];
%         eye_mov_ons(eye_mov_ons+10>length(cur_delta))=[];
%         eye_mov_ons(eye_mov_ons-10<0)=[];
        rem=zeros(1,length(eye_mov_ons));
        for ind=1:length(eye_mov_ons)
            for gnd=1:size(blink_snips{tnd},1)
                if sum(blink_snips{tnd}(gnd,1):blink_snips{tnd}(gnd,2)==eye_mov_ons(ind))>0
                    rem(ind)=1;
                end
            end
        end
        eye_mov_ons(logical(rem))=[];
        if ynd>1
            eye_mov_ons(ismember(eye_mov_ons,eye_ons_all))=[];
            eye_ons_all(end+1:end+length(eye_mov_ons))=eye_mov_ons;
        else
            eye_ons_all=eye_mov_ons;
        end
    end
    eye_ons_all=sort(eye_ons_all);
    eye_ons_all(find(diff(eye_ons_all)<data_lwin+data_rwin)+1)=[];
    eye_ons_all(eye_ons_all+data_rwin>length(cur_delta))=[];
    eye_ons_all(eye_ons_all-data_lwin<0)=[];
    eye_ons_all=eye_ons_all+max(exp_inds{tnd}(order{tnd}~=3,2));
    cnt=1;
    eye_snips_dark{tnd}=[];
    if ~isempty(eye_ons_all)
        for fnd=1:length(eye_ons_all)
            if sum(runM(eye_ons_all(fnd)-l_win:eye_ons_all(fnd)+r_win))==0 %| sum(runM(eye_ons_all(fnd)-l_win:eye_ons_all(fnd)+r_win))==1
                eye_snips_dark{tnd}(cnt,1)=eye_ons_all(fnd)-data_lwin;
                eye_snips_dark{tnd}(cnt,2)=eye_ons_all(fnd)+data_rwin;
                cnt=cnt+1;
            end
        end
    else
        eye_snips_dark{tnd}=[];
    end
end

for lnd=1:length(blink_snips)
    b_rem=[];
    e_rem=[];
    nbr_frames=proj_meta(cur_site).rd(1,lnd).nbr_frames;
    for hnd=1:length(nbr_frames)-1
        cur_frame=sum(nbr_frames(1:hnd));
        if ~isempty(blink_snips{lnd})
            for jnd=1:size(blink_snips{lnd},1)
                if sum((blink_snips{lnd}(jnd,1):blink_snips{lnd}(jnd,2))==cur_frame)>0
                    b_rem(jnd,hnd)=1;
                else
                    b_rem(jnd,hnd)=0;
                end
            end
        end
        if ~isempty(eye_snips{lnd})
            for jnd=1:size(eye_snips{lnd},1)
                if sum((eye_snips{lnd}(jnd,1):eye_snips{lnd}(jnd,2))==cur_frame)>0
                    e_rem(jnd,hnd)=1;
                else
                    e_rem(jnd,hnd)=0;
                end
            end
        end
    end
    b_rem=sum(b_rem,2);
    e_rem=sum(e_rem,2);
    blink_snips{lnd}(logical(b_rem),:)=[];
    eye_snips{lnd}(logical(e_rem),:)=[];
end